const http = require('http')
const path = require('path')
const fs = require('fs')
const url = require('url')
const querystring = require('querystring')
const webroot = path.join(__dirname, 'src')
http.createServer((req, res) => {
    let uri = req.url
    let { pathName, query } = url.parse(uri, true)
    let extname = path.extname(pathName)
    if ('favicon.ico' != pathName && req.method == 'GET') {
        pathName = pathName == '/' ? 'index.html' : pathName
    }
    // 设置文件路径。针对非html文件
    let filepath = path.join(webroot, pathName)
    if ('.html' == extname) {
        filepath = path.join(webroot, 'src', pathName)
        fs.readFileSync(filepath, 'utf-8', (err, data) => {
            if (err) {
                res.statusCode = 404
                res.end('not found')
            } else {
                res.end(data)
            }
        })
    }else{
        switch(pathName){
            case '/login':
                let data=''
                req.on('data',chunk=>data+=chunk)
                req.on('end',()=>{
                    res.end('post')
                })
                break;
            case '/news':
                let data=[]
                req.on('data',chunk=>data.push(chunk))
                req.on('end',()=>{
                    let postData=Buffer.concat(data).toString()
                    res.end(post)
                })
                break;
            default:
                res.end('not')
        }
    }
}).listen(8082,()=>{
    console.log('post or get')
})
